package com.mf.controller.queue;

import lombok.Getter;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @param name  任务名称
 * @param delay 延迟时间
 * @Author: lmf
 * @Create: 2025/5/22 22:28
 * @module 延迟任务
 */
public record DelayTask(String name, long delay) implements Delayed {
    public DelayTask(String name, long delay) {
        this.name = name;
        this.delay = System.currentTimeMillis() + delay;
    }

    /**
     * 获取延迟时间
     *
     * @param unit the time unit
     * @return 当前延迟时间
     */
    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(delay - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return Long.compare(getDelay(TimeUnit.MILLISECONDS), o.getDelay(TimeUnit.MILLISECONDS));
    }
}
